/* -*- C++ -*- */
#ifndef __draw_Mgr_Header_File__
#define __draw_Mgr_Header_File__

#include "common.h"

namespace core
{
	namespace draw
	{

		/*
		class Item
		{
				public:
				struct Loader
				{
						Item* operator()(const std::string& name)
						{
								std::cout << "Loading: " << name << std::endl;
		
								return new Item();
						}
				};
		};
		}
		*/
		
		
		/**
		 * Template for manager classes. Provides clear and nice
		 * mechanism to manage loading of items sysmem wide.
		 * 
		 * 
		 */
		template <class Item>
		class Mgr {
		  
		  typename Item::Loader loader;
		  
		  typedef std::map<std::string, Item*> Map;
		  typedef typename Map::iterator Iterator;
		  typedef typename Map::const_iterator ConstIterator;
		  
		  Map data;
		  static Mgr* inst;
		  
		  Mgr(){}
		  
		public:
		  
		  Item* operator[](const std::string& name)  {
		    //std::cout << "Accessing: " << name <<std::endl;
		    ConstIterator it = data.find(name);
		    if (it != data.end()) {
		      return ((*it).second);
		    }
		    else {
		      Item* newItem = loader(name);
		      data[name] = newItem;
		      return newItem;
		    }
		  }
		  
		  static Mgr<Item>& instance() {
		    if (!inst) {
		      inst = new Mgr<Item>();
						}
		    return *inst;
		  }
		};
		
		
		
		
	}
}


#endif  //__draw_Mgr_Header_File__


